unit uFrmHall;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ExtCtrls, Oracle, ufrmtable, uFrmMainLPT, OracleData, StdCtrls, ufrmloading,
  jpeg, uimagepanel, DB, pngimage, gnugettext, uutils, umainutils, DBCtrls, strutils,
  Buttons, inifiles, GR32_Image, ufrmdealerselect, usqls, ScktComp;

type
  TFrmHall = class(TForm)
    pgc_Hall: TPageControl;
    tbs_Hall: TTabSheet;
    tbs_Table: TTabSheet;
    pnl_TopHall: TPanel;
    pnl_TableDetail: TPanel;
    img_SmallTable: TImage;
    img_BigTable: TImage;
    pnl_CashGame: TPanel;
    pnl_CashGameCol1: TPanel;
    pnl_CashGameCol2: TPanel;
    pnl_CashGameCol3: TPanel;
    pnl_CashGameCol4: TPanel;
    pnl_CashGameCaption: TPanel;
    pnl_CashGameCol5: TPanel;
    pnl_TablesCol2: TPanel;
    pnl_TablesCol3: TPanel;
    pnl_TablesCol4: TPanel;
    pnl_TablesCol5: TPanel;
    OracleDataSetBreakSeats: TOracleDataSet;
    pnl_TablesCol1: TPanel;
    OracleDataSetHallZone: TOracleDataSet;
    DataSourceHallZone: TDataSource;
    com_HallZone: TDBLookupComboBox;
    Label3: TLabel;
    btn_Back: TSpeedButton;
    OracleQuery1: TOracleQuery;
    OracleSession1: TOracleSession;
    tmr_ScrollMsg: TTimer;
    pnl_Messages: TPanel;
    lbl_Message: TLabel;
    img_Background: TImage;
    pnl_TournamentsCaption: TLabel;
    img_Inspector2: TImagePanel;
    img_Inspector3: TImagePanel;
    ClientSocket1: TClientSocket;
    img_Inspector1: TImagePanel;
    img_Inspector: TImage;
    procedure btn_BackClick(Sender: TObject);
    procedure com_HallZoneClick(Sender: TObject);
    procedure tmr_ScrollMsgTimer(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
    procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure FormDestroy(Sender: TObject);
    procedure img_Inspector1Click(Sender: TObject);
  private
    { Private declarations }
    m_FrmTable: TFrmTable;
    // MainUtils: TMainUtils;
    procedure GetBreakTableData(TableID: string; HavePlayers: boolean);
    procedure StopFlicker(var Msg: TWMEraseBkgnd);
    message WM_ERASEBKGND;
    procedure GetLoggedInspectors();
    procedure SendEventToServer(EventType, TournamentID, TableID: string);
  public
    m_FrmTableDetail: TFrmMainLPT;
    IsEmbeded: boolean;
    SomethingChanged: boolean;
    procedure GetData(OracleSession: TOracleSession);
    procedure OnTableClick(Sender: TObject);
    function BreakinTableFinished(TournamentID: string): boolean;
    procedure PopulateHallZoneCombo;
    procedure Init(OraSession: TOracleSession);
    procedure CheckMessages();
    procedure GetIniFile();
    { Public declarations }
  end;

var
  FrmHall: TFrmHall;

implementation

uses uFrmSubjectImage;

// uses ufrmmain;
{$R *.dfm}
{ TFrmHall }

procedure TFrmHall.StopFlicker(var Msg: TWMEraseBkgnd);
begin
  Msg.Result := 1;
end;

procedure TFrmHall.PopulateHallZoneCombo;
begin
  OracleDataSetHallZone.CloseAll;
  OracleDataSetHallZone.Session := OraSessMainUtils;
  OracleDataSetHallZone.SQL.Clear;
  OracleDataSetHallZone.SQL.Text := ('select * from TABLE_ZONES where disabled=0 order by NAME ASC');
  // ---
  OracleDataSetHallZone.Open;
  DataSourceHallZone.DataSet := OracleDataSetHallZone;
  com_HallZone.ListSource := DataSourceHallZone;
  com_HallZone.KeyField := 'ID';
  com_HallZone.ListField := 'NAME';
  com_HallZone.KeyValue := OracleDataSetHallZone.FieldValues['ID'];
end;

procedure TFrmHall.tmr_ScrollMsgTimer(Sender: TObject);
begin
  if lbl_Message.Left + Canvas.TextWidth(lbl_Message.Caption) < 0 then
  BEGIN
    lbl_Message.Left := SELF.Width;
  END
  else
  begin
    lbl_Message.Left := lbl_Message.Left - 2;
  end;
  lbl_Message.Repaint;
end;

procedure TFrmHall.SendEventToServer(EventType, TournamentID, TableID: string);
begin
  try
    if not ClientSocket1.Active then
    begin
      ClientSocket1.Open;
      if ParamStr(3) <> 'LOGIN' then
        ClientSocket1.Active := true;
    end;
  finally
    ClientSocket1.Socket.SendText(EventType + '|1|' + TournamentID + '|2|' + TableID);
  end;
end;

procedure TFrmHall.OnTableClick(Sender: TObject);
var
  TableID, TableType: string;
  HavePlayers: boolean;
begin
  if Sender is TImage then
  begin
    TableID := TImage(Sender).Parent.Hint;
    TableType := TImage(Sender).Parent.Name;
  end
  else if Sender is TLabel then
  begin
    TableID := TLabel(Sender).Parent.Hint;
    TableType := TLabel(Sender).Parent.Name;
  end;
  if TableID <> '' then
  begin
    LockWindowUpdate(SELF.handle);
    if m_FrmTableDetail = nil then
    begin
      m_FrmTableDetail := TFrmMainLPT.Create(SELF);
      m_FrmTableDetail.Parent := pnl_TableDetail;
      m_FrmTableDetail.BorderStyle := BSnONE;
      m_FrmTableDetail.Top := 0;
      m_FrmTableDetail.Left := 0;
      m_FrmTableDetail.FrmTable.img_Table.Picture := (img_BigTable.Picture);
      m_FrmTableDetail.OracleQuery1.Session := OraSessMainUtils;
      m_FrmTableDetail.OracleDataSet1.Session := OraSessMainUtils;
      m_FrmTableDetail.FrmTable.OracleQueryTable.Session := OraSessMainUtils;
      m_FrmTableDetail.FrmTable.ods_BreakTable.Session := OraSessMainUtils;
      m_FrmTableDetail.FrmTable.ods_FreeTablePositions.Session := OraSessMainUtils;
      m_FrmTableDetail.FrmTable.ClientSocket1.Host := ClientSocket1.Host;
      m_FrmTableDetail.FrmTable.ClientSocket1.Port := ClientSocket1.Port;
      if ParamStr(3)<>'LOGIN' then
      m_FrmTableDetail.FrmTable.ClientSocket1.Active := true;
    end;
    pgc_Hall.ActivePage := tbs_Table;
    HavePlayers := false;
    if (TableType = 'CASHGAME') then
    begin
      m_FrmTableDetail.pnl_BreakTable.Visible := false;
      m_FrmTableDetail.FrmMainClock.Visible := false;
      m_FrmTableDetail.tmr_ClockUpdate.Enabled := false;
      m_FrmTableDetail.FrmTable.GetData(TableID, true, OracleDataSetCashGame, OracleDataTables);
    end
    else
    begin
      m_FrmTableDetail.pnl_BreakTable.Visible := true;
      HavePlayers := m_FrmTableDetail.FrmTable.GetData(TableID, false, OracleDataSetTournament, OracleDataTables);
      if m_FrmTableDetail.FrmTable.lbl_Tournament.Caption <> '' then
      begin
        m_FrmTableDetail.FrmMainClock.QueryTimes := 0;
        m_FrmTableDetail.tmr_ClockUpdate.Interval := 1;
        m_FrmTableDetail.tmr_ClockUpdate.Enabled := true;
        m_FrmTableDetail.FrmMainClock.Visible := true;
      end
      else
      begin
        m_FrmTableDetail.FrmMainClock.Visible := false;
      end;
    end;
    m_FrmTableDetail.FrmMainClock.GetClockData(m_FrmTableDetail.FrmTable.m_TournamentID, '');
    m_FrmTableDetail.DeselectMenuButtons();
    GetBreakTableData(TableID, HavePlayers);
    m_FrmTableDetail.FrmTable.pgc_Table.ActivePage := m_FrmTableDetail.FrmTable.tbs_Table;
    m_FrmTableDetail.FrmTable.pnl_PlayerMenu.Visible := false;
    m_FrmTableDetail.lbl_TOPTournament.Caption := m_FrmTableDetail.FrmTable.lbl_Tournament.Caption;
    m_FrmTableDetail.lbl_TOPTableName.Caption := Trim(m_FrmTableDetail.FrmTable.lbl_Table.Caption);
    m_FrmTableDetail.Show;
    pnl_Messages.Visible := false;
    btn_Back.Parent := m_FrmTableDetail.pnl_TopTable;
    btn_Back.Top := 0;
    btn_Back.BringToFront;
    // check if inspector button pressed
    OracleQuery1.Session := OraSessMainUtils;
    OracleQuery1.Close;
    OracleQuery1.SQL.Clear;
    OracleQuery1.SQL.Text := ('SELECT VERIFIED FROM INSPECTOR_CALLS WHERE VERIFIED=0 AND TABLE_ID=' + TableID);
    OracleQuery1.Execute;
    // lbl_Message.Top := m_FrmTableDetail.clientHeight - m_FrmTableDetail.pnl_Bottom.clientHeight - lbl_Message.clientHeight + 7;
    lbl_Message.BringToFront;
    if not OracleQuery1.Eof then
    begin
      m_FrmTableDetail.pnl_Inspector.Color := BottomPanelDownColor;
      m_FrmTableDetail.FrmTable.CheckMessages();
    end;
    LockWindowUpdate(0);
  end;
end;

procedure TFrmHall.btn_BackClick(Sender: TObject);
begin
  LockWindowUpdate(SELF.handle);
  GetData(OraSessMainUtils);
  pgc_Hall.ActivePage := tbs_Hall;
  LockWindowUpdate(0);
end;

procedure TFrmHall.com_HallZoneClick(Sender: TObject);
begin
  GetData(OraSessMainUtils);
  pgc_Hall.ActivePage := tbs_Hall;
end;

procedure TFrmHall.FormDestroy(Sender: TObject);
begin
  ClientSocket1.Close;
  FreeAndNil(pct_Temp);
end;

procedure TFrmHall.FormResize(Sender: TObject);
begin
  IsEmbeded := UpperCase(ExtractFileName(Application.ExeName)) = 'LPM.EXE';
  if not IsEmbeded then
  begin
    if pct_Temp = nil then
      pct_Temp := TPicture.Create;
    GetIniFile();
    SetOracleSession(SELF, OracleSession1, '', '');
    PopulateHallZoneCombo();
    GetData(OraSessMainUtils)
  end;
end;

procedure TFrmHall.GetIniFile();
var
  appINI: Tinifile;
  iniFileName: string;
begin
  iniFileName := ChangeFileExt(Application.ExeName, '.ini');
  if not FileExists(iniFileName) then
  begin
    FileCreate(iniFileName);
  end
  else
  begin
    appINI := Tinifile.Create(iniFileName);
    try
      OracleSession1.LogonUsername := appINI.ReadString('Database', 'User', '');
      OracleSession1.LogonPassword := appINI.ReadString('Database', 'Pass', '');
      OracleSession1.LogonDatabase := appINI.ReadString('Database', 'TNS', '');
      ClientSocket1.Port := appINI.ReadInteger('RefreshService', 'Port', 0);
      ClientSocket1.Host := appINI.ReadString('RefreshService', 'Host', '');
      if ParamStr(3) <> 'LOGIN' then
        ClientSocket1.Active := true;
      OracleSession1.logon;
      Init(OracleSession1);
      // GetData(OracleSession1);
    finally
      appINI.free;
    end;
  end;
end;

procedure TFrmHall.GetLoggedInspectors();
var
  TableZoneId: string;
  i: Integer;
begin
  for i := ComponentCount - 1 downto 0 do
  begin
    if Components[i] is TImagePanel then
    begin
      TImagePanel(Components[i]).Image.Picture := img_Inspector.Picture;
      TImagePanel(Components[i]).Image.OnClick := img_Inspector1Click; ;
      TImagePanel(Components[i]).PlayerStatus.OnClick := img_Inspector1Click;
      TImagePanel(Components[i]).PlayerName.OnClick := img_Inspector1Click;
      TImagePanel(Components[i]).Caption := '';
      TImagePanel(Components[i]).PlayerName.Caption := '';
      TImagePanel(Components[i]).Image.Hint := '';
    end;
  end;
  TableZoneId := com_HallZone.KeyValue;
  if TableZoneId <> '' then
  begin
    OracleQuery1.Session := OraSessMainUtils;
    OracleQuery1.Close;
    OracleQuery1.SQL.Clear;
    OracleQuery1.SQL.Text := ('SELECT * FROM GET_LOGGED_ZONE_SUBJECTS');
    OracleQuery1.Execute;
    img_Inspector1.Image.Picture := img_Inspector.Picture;
    img_Inspector2.Image.Picture := img_Inspector.Picture;
    img_Inspector3.Image.Picture := img_Inspector.Picture;
    if not OracleQuery1.Eof then
    begin
      img_Inspector1.PlayerName.Caption := VarToStr(OracleQuery1.Field('SUBJECT'));
      FrmSubjectImage.img_TakenPhoto.Picture.Assign(uutils.GetImage(SQL_User_Picture + VarToStr(OracleQuery1.Field('USER_ID')), OraSessMainUtils, false));
      FrmSubjectImage.img_BlackHole.BringToFront;
      img_Inspector1.Image.Transparent := true;
      img_Inspector1.Image.Picture.Bitmap.Assign(FrmSubjectImage.GetFormImage);
      img_Inspector1.Image.Picture.Bitmap.PixelFormat := pf24Bit;
      img_Inspector1.Image.Hint := VarToStr(OracleQuery1.Field('USER_ID'));
      OracleQuery1.Next;
    end;
    if not OracleQuery1.Eof then
    begin
      img_Inspector2.PlayerName.Caption := VarToStr(OracleQuery1.Field('SUBJECT'));
      img_Inspector2.OnClick := img_Inspector1Click;
      FrmSubjectImage.img_TakenPhoto.Picture.Assign(uutils.GetImage(SQL_User_Picture + VarToStr(OracleQuery1.Field('USER_ID')), OraSessMainUtils, false));
      FrmSubjectImage.img_BlackHole.BringToFront;
      img_Inspector2.Image.Transparent := true;
      img_Inspector2.Image.Picture.Bitmap.Assign(FrmSubjectImage.GetFormImage);
      img_Inspector2.Image.Picture.Bitmap.PixelFormat := pf24Bit;
      img_Inspector2.Image.Hint := VarToStr(OracleQuery1.Field('USER_ID'));
      OracleQuery1.Next;
    end;
    if not OracleQuery1.Eof then
    begin
      img_Inspector3.PlayerName.Caption := VarToStr(OracleQuery1.Field('SUBJECT'));
      img_Inspector3.OnClick := img_Inspector1Click;
      FrmSubjectImage.img_TakenPhoto.Picture.Assign(uutils.GetImage(SQL_User_Picture + VarToStr(OracleQuery1.Field('USER_ID')), OraSessMainUtils, false));
      FrmSubjectImage.img_BlackHole.BringToFront;
      img_Inspector3.Image.Transparent := true;
      img_Inspector3.Image.Picture.Bitmap.Assign(FrmSubjectImage.GetFormImage);
      img_Inspector3.Image.Picture.Bitmap.PixelFormat := pf24Bit;
      img_Inspector3.Image.Hint := VarToStr(OracleQuery1.Field('USER_ID'));
      OracleQuery1.Next;
    end;
  end;
end;

procedure TFrmHall.img_Inspector1Click(Sender: TObject);
var
  UserID: string;
begin
  UserID := TImage(Sender).Hint;
  if UserID = '' then // sender is player status label
  begin
    UserID := TImagePanel(TLabel(Sender).Parent).Image.Hint;
  end;
  if UserID = '' then
    UserID := '-1';
  // check if inspector is looged in
  OracleQuery1.SQL.Clear;
  OracleQuery1.SQL.Text := 'SELECT * FROM TABLE_ZONE_SUBJECT_LOGINS WHERE IN_DATETIME IS NOT NULL AND OUT_DATETIME IS NULL AND TABLE_ZONE_ID=' + VarToStr(com_HallZone.KeyValue)
    + ' AND USER_ID=' + UserID;
  OracleQuery1.Execute;
  if OracleQuery1.Eof then
  begin
    // login inspector
    FrmDealerSelect.ShowDealerForm('', '', '', VarToStr(com_HallZone.KeyValue),UserID);
    SendEventToServer(EventTypeInspectorChange, '', '');
    GetLoggedInspectors();
  end
  else
  begin
    // logout inspector
    if MessageDlg(_('Inspector out?' + '(' + TImagePanel(TLabel(Sender).Parent).PlayerName.Caption + ')'), mtWarning, [mbYes, mbNo], 0) = mrYes then
    begin
      UpdateDataToDB('TABLE_ZONE_SUBJECT_LOGINS', 'OUT_DATETIME=(SELECT SYSDATE FROM DUAL)', 'TABLE_ZONE_ID=' + VarToStr(com_HallZone.KeyValue) + ' AND OUT_DATETIME IS NULL AND USER_ID=' + UserID);
      SendEventToServer(EventTypeInspectorChange, '', '');
      GetLoggedInspectors();
    end;
  end;
end;

procedure TFrmHall.Init(OraSession: TOracleSession);
begin
  if pct_Temp = nil then
    pct_Temp := TPicture.Create();
  SetOracleSession(SELF, OraSession, '', '');
  PopulateHallZoneCombo();
  GetData(OraSessMainUtils)
end;

procedure TFrmHall.CheckMessages();
begin
  // check messages
  OracleQuery1.Session := OraSessMainUtils;
  OracleQuery1.Close;
  OracleQuery1.SQL.Clear;
  OracleQuery1.SQL.Text := ('select name from INSPECTOR_CALLS ic join tables ta on ic.table_id=ta.id where verified=0');
  OracleQuery1.Execute;
  lbl_Message.Caption := '';
  tmr_ScrollMsg.Enabled := false;
  while not OracleQuery1.Eof do
  begin
    lbl_Message.Caption := lbl_Message.Caption + OracleQuery1.Field(0) + ', ';
    OracleQuery1.Next;
    tmr_ScrollMsg.Enabled := true;
  end;
  if lbl_Message.Caption <> '' then
  begin
    lbl_Message.Caption := _('Call Inspector from') + ' ' + lbl_Message.Caption;
  end;
  /// //
end;

procedure TFrmHall.ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
  ErrorCode := 0; // this line solve your problems about asyncroneus socket error
end;

procedure TFrmHall.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
var
  EventType, TournamentID, TableID: string;
begin
  uutils.SocketTextParse(Socket.ReceiveText, EventType, TournamentID, TableID);
  if ((EventType = EventTypeTicket) or (EventType = EventTypeFromTable)) and (pgc_Hall.ActivePage = tbs_Hall) then
  begin
    GetData(OraSessMainUtils);
  end
  else if (EventType = EventTypeInspectorChange) then
  begin
    GetLoggedInspectors();
  end
  else if (EventType = EventTypeCallIns) and (pgc_Hall.ActivePage = tbs_Hall) then
  begin
    CheckMessages();
  end;
end;

procedure TFrmHall.GetData(OracleSession: TOracleSession);
var
  i, j, TablesCount, CashTablesCount: Integer;
  TableID: string;
  IsCashGame: boolean;
  DataSet: TOracleDataSet;
begin
  if com_HallZone.KeyValue = null then
  begin
    exit;
  end;
  LockWindowUpdate(self.Handle);
  if FrmSubjectImage = nil then
    FrmSubjectImage := TFrmSubjectImage.Create(SELF);
  FrmLoading.Show;
  FrmLoading.ProgressBar1.Position := 10;
  FrmLoading.Repaint;
  pnl_Messages.Visible := true;
  lbl_Message.BringToFront;
  // if not IsEmbeded then
//  SendMessage(SELF.handle, WM_SETREDRAW, 0, 0);
  CheckMessages();
  DataSet := TOracleDataSet.Create(SELF);
  DataSet.Session := OraSessMainUtils;
  DataSet.Close;
  DataSet.SQL.Clear;
  DataSet.SQL.Text :=
    'select ID AS TABLE_ID, NAME, SEATS, NVL(CASH_GAME,0)AS CASH_GAME, NVL(MUST_SET_DEALER,0)AS MUST_SET_DEALER, NVL(IS_BREAK_TABLE,0)AS IS_BREAK_TABLE from tables ta where table_zone_id=' + VarToStr
    (com_HallZone.KeyValue) + '  and disabled=0 order by name asc';
  DataSet.Open;
  // -----
  TablesCount := 0;
  CashTablesCount := 0;
  GetHallDataSetsData();
  FrmLoading.ProgressBar1.Position := 20;
  FrmLoading.Repaint;
  pgc_Hall.ActivePage := tbs_Hall;
  // clear tournament tables
  for i := tbs_Hall.ControlCount - 1 downto 0 do
  begin
    if tbs_Hall.Controls[i] is TPanel then
    begin
      if LeftStr(TPanel(tbs_Hall.Controls[i]).Name, 13) = 'pnl_TablesCol' then
      begin
        for j := TPanel(tbs_Hall.Controls[i]).ControlCount - 1 downto 0 do
        begin
          TFrmTable(TPanel(tbs_Hall.Controls[i]).Controls[j]).Close;
          TFrmTable(TPanel(tbs_Hall.Controls[i]).Controls[j]).free;
        end;
      end;
    end;
  end;
  // clear cash games
  for i := pnl_CashGame.ControlCount - 1 downto 0 do
  begin
    if pnl_CashGame.Controls[i] is TPanel then
    begin
      if LeftStr(TPanel(pnl_CashGame.Controls[i]).Name, 12) = 'pnl_CashGame' then
      begin
        for j := TPanel(pnl_CashGame.Controls[i]).ControlCount - 1 downto 0 do
        begin
          TFrmTable(TPanel(pnl_CashGame.Controls[i]).Controls[j]).Close;
          TFrmTable(TPanel(pnl_CashGame.Controls[i]).Controls[j]).free;
        end;
      end;
    end;
  end;
  FrmLoading.ProgressBar1.Position := 50;
  FrmLoading.Repaint;
  pnl_CashGame.Visible := false;
  pnl_TournamentsCaption.Visible := false;
  while not DataSet.Eof do
  begin
    m_FrmTable := TFrmTable.Create(SELF);
    if VarToStr(DataSet.FieldValues['CASH_GAME']) = '1' then
    begin
      pnl_CashGame.Visible := true;
      inc(CashTablesCount);
      if CashTablesCount in [1, 6] then
      begin
        m_FrmTable.Parent := pnl_CashGameCol1;
      end
      else if CashTablesCount in [2, 7] then
      begin
        m_FrmTable.Parent := pnl_CashGameCol2;
      end
      else if CashTablesCount in [3, 8] then
      begin
        m_FrmTable.Parent := pnl_CashGameCol3;
      end
      else if CashTablesCount in [4, 9] then
      begin
        m_FrmTable.Parent := pnl_CashGameCol4;
      end
      else if CashTablesCount in [5, 10] then
      begin
        m_FrmTable.Parent := pnl_CashGameCol5;
      end;
    end
    else // TOURNAMENT
    begin
      pnl_TournamentsCaption.Visible := true;
      inc(TablesCount);
      if TablesCount in [1, 6, 11, 16, 21, 26, 31] then
      begin
        m_FrmTable.Parent := pnl_TablesCol1;
      end
      else if TablesCount in [2, 7, 12, 17, 22, 27, 32] then
      begin
        m_FrmTable.Parent := pnl_TablesCol2;
      end
      else if TablesCount in [3, 8, 13, 18, 23, 28, 33] then
      begin
        m_FrmTable.Parent := pnl_TablesCol3;
      end
      else if TablesCount in [4, 9, 14, 19, 24, 29, 34] then
      begin
        m_FrmTable.Parent := pnl_TablesCol4;
      end
      else if TablesCount in [5, 10, 15, 20, 25, 30, 35] then
      begin
        m_FrmTable.Parent := pnl_TablesCol5;
      end;
    end;
    m_FrmTable.Align := altop;
    m_FrmTable.img_Table.OnClick := OnTableClick;
    m_FrmTable.lbl_Tournament.OnClick := OnTableClick;
    m_FrmTable.lbl_Table.OnClick := OnTableClick;
    m_FrmTable.lbl_TableStatus.OnClick := OnTableClick;
    m_FrmTable.lbl_TableStatus.Font.Size := 9;
    m_FrmTable.lbl_TableStatus.Top := m_FrmTable.img_Table.Height - m_FrmTable.lbl_TableStatus.Height * 2;
    m_FrmTable.lbl_TableStatus.Left := 15;
    m_FrmTable.Top := 100;
    TableID := VarToStr(DataSet.FieldValues['TABLE_ID']);
    if TableID <> '' then
    begin
      IsCashGame := (VarToStr(DataSet.FieldValues['CASH_GAME']) = '1');
      if IsCashGame then
      begin
        m_FrmTable.GetData(TableID, IsCashGame, OracleDataSetCashGame, OracleDataTables);
      end
      else
      begin
        m_FrmTable.GetData(TableID, IsCashGame, OracleDataSetTournament, OracleDataTables);
      end;
      if m_FrmTable.Width > m_FrmTable.SmallTableWidth then
      begin
        m_FrmTable.img_Table.Picture := (img_BigTable.Picture);
      end
      else
      begin
        m_FrmTable.img_Table.Picture := (img_SmallTable.Picture);
      end;
      m_FrmTable.tbs_Table.Hint := TableID;
      if IsCashGame then
      begin
        m_FrmTable.tbs_Table.Name := 'CASHGAME';
      end;
      m_FrmTable.Show;
      m_FrmTable.MakeTransparent;
    end;
    DataSet.Next;
    pnl_CashGame.Height := pnl_CashGameCaption.Height + (m_FrmTable.clientHeight + m_FrmTable.Margins.Top + m_FrmTable.Margins.Bottom) * pnl_CashGameCol1.ControlCount;
  end;
  FrmLoading.Close;
  DataSet.Close;
  FreeAndNil(DataSet);
  GetLoggedInspectors();
//  SendMessage(SELF.handle, WM_SETREDRAW, 1, 0);
  LockWindowUpdate(0);
end;

procedure TFrmHall.GetBreakTableData(TableID: string; HavePlayers: boolean);
begin
  m_FrmTableDetail.pnl_BreakTable.Color := BottomPanelUpColor;
  OracleQuery1.Session := OraSessMainUtils;
  OracleQuery1.Close;
  OracleQuery1.SQL.Clear;
  OracleQuery1.SQL.Text := ('SELECT MUST_SET_DEALER, IS_BREAK_TABLE FROM TABLES WHERE ID=' + TableID);
  OracleQuery1.Execute;
  if OracleQuery1.Field(1) = '1' then
  begin
    m_FrmTableDetail.pnl_BreakTable.Color := BottomPanelDownColor;
    m_FrmTableDetail.FrmTable.CheckMessages();
  end;
  if (not m_FrmTableDetail.FrmTable.m_IsCashGame) and (m_FrmTableDetail.FrmTable.m_TournamentID <> '') then
  begin
    OracleQuery1.Close;
    OracleQuery1.SQL.Clear;
    OracleQuery1.SQL.Text := ('SELECT MUST_SET_DEALER, IS_BREAK_TABLE, GPP.PLAYING FROM TABLES TA JOIN GET_PLAYERS_PLAY_TICK_BY_TABLE GPP ON GPP.TABLE_ID=TA.ID WHERE TA.ID=' + TableID +
        ' AND GPP.PLAYING<>0 AND GPP.TOURNAMENT_ID=' + m_FrmTableDetail.FrmTable.m_TournamentID);
    OracleQuery1.Execute;
    if not OracleQuery1.Eof then
    begin
      if OracleQuery1.Field(0) = '1' then
      begin
        // pnl_TableStatus.Caption := _('Select Dealer position!');
      end;
      if OracleQuery1.Field(1) = '1' then
      begin
        // pnl_TableStatus.Caption := _('Table is selected for breaking!');
        m_FrmTableDetail.pnl_BreakTable.Color := BottomPanelDownColor;
        m_FrmTableDetail.FrmTable.CheckMessages();
      end;
      if (OracleQuery1.Field(1) = '0') and (OracleQuery1.Field(0) = '1') then
      begin
        // pnl_TableStatus.Caption := _('Select Dealer position! Break Table.');
        m_FrmTableDetail.pnl_BreakTable.Color := BottomPanelDownColor;
        m_FrmTableDetail.FrmTable.CheckMessages();
      end;
    end;
    // check if break is not finished
    if m_FrmTableDetail.FrmTable.m_TournamentID <> '' then
    begin
      if BreakinTableFinished(m_FrmTableDetail.FrmTable.m_TournamentID) then
      begin
        // btn_BreakTable.Visible := True;
      end;
    end;
  end;
end;

function TFrmHall.BreakinTableFinished(TournamentID: string): boolean;
begin
  Result := true;
  OracleQuery1.Close;
  OracleQuery1.SQL.Clear;
  OracleQuery1.SQL.Text := ('SELECT COUNT(1) FROM TOURNAMENT_TABLES TT JOIN TABLES TA ON TA.ID=TT.TABLE_ID WHERE TOURNAMENT_ID=' + TournamentID + ' AND IS_BREAK_TABLE=1');
  OracleQuery1.Execute;
  if OracleQuery1.Field(0) = 1 then
  begin
    Result := false;
  end;
end;

end.
